package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.Vo.RechargeVo;
import cn.leyaber.hrm.clients.UserFeignClient;
import cn.leyaber.hrm.domain.Recharge;
import cn.leyaber.hrm.domain.Wholename;
import cn.leyaber.hrm.query.RechargeQuery;
import cn.leyaber.hrm.service.IRechargeService;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/recharge")
public class RechargeController {
    @Autowired
    public IRechargeService rechargeService;
    @Autowired
    private UserFeignClient userFeignClient;

   @PostMapping(value="/Modify_status")
    public AjaxResult Modify_status(@RequestBody Recharge recharge){
        try {
            rechargeService.Modify_statusd(recharge);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改对象失败！"+e.getMessage());
        }
    }

    /**
    * 保存和修改公用的
    * @param recharge  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Recharge recharge){
        try {
            if(recharge.getId()!=null){
                rechargeService.updateById(recharge);
            }else{
                rechargeService.insert(recharge);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            rechargeService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Recharge get(@PathVariable("id")Long id)
    {
        return rechargeService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Recharge> list(){

        return rechargeService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<RechargeVo> json(@RequestBody RechargeQuery query)
    {
        Page<Recharge> page = new Page<Recharge>(query.getPage(),query.getRows());
        page = rechargeService.selectPage(page);
        Page<RechargeVo> page1 = JSON.parseObject(JSON.toJSONString(page), Page.class);
        ArrayList<RechargeVo> objects = new ArrayList<>();
        for (Recharge record : page.getRecords()) {
            RechargeVo rechargeVo = JSON.parseObject(JSON.toJSONString(record), RechargeVo.class);
            if (StringUtils.isEmpty(record.getUserWholenameId())){
                objects.add(rechargeVo);
                continue;
            }
            Wholename wholename = userFeignClient.selectById(record.getUserWholenameId());
            rechargeVo.setUser(wholename.getUser());
        }
        page1.setRecords(objects);
        return new PageList<RechargeVo>(page1.getTotal(),page1.getRecords());
    }
}
